package com.china.lottery.listener.task.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.china.lottery.listener.dao.DiaryInfoDao;
import com.china.lottery.listener.dao.DictionaryDao;
import com.china.lottery.listener.dao.entity.LAgentConfig;
import com.china.lottery.listener.dao.entity.LChannelInfo;
import com.china.lottery.listener.task.core.BaseNoteService;
import com.china.lottery.listener.util.Constant;
import com.china.lottery.listener.util.HttpClient3;
import com.china.lottery.listener.util.Tools;

/**
 * 渠道余额Service
 * 
 * @date 2015年4月8号
 * @author ch
 */
@Component("channelBanlanceSynService")
public class ChannelBanlanceService extends BaseNoteService {

	private static String NUMCODE = Constant.CHANNELBANLANCE;
//	private static String CHANNELURL = Constant.URL;

	@Autowired
	private DictionaryDao dicDao;

	@Autowired
	private DiaryInfoDao diaryInfoDao;

	public static final String wuCaiUrl=Constant.wuCaiUrl+"lottery!queryBalance";
	
	@Override
	public List<String> getNumbers() {

		return this.getNumberFromParent(dicDao.findByParentCode(NUMCODE));
	}

	@Override
	public String getContent() {
		StringBuffer msgContent = new StringBuffer();
		// 获取前一天日期
		Date startdate = this.getPreDate();
		Date enddate = Tools.getCurrentShortFormatDate();
		String channel=this.getChannelMoney(startdate,enddate);
		String agent=this.agentInfo(startdate,enddate);
		String time = Tools.shortFormat(startdate);
		if(!Tools.isBlank(agent)){
			msgContent.append("(代理商").append(time).append("日交易余额:)" + agent);
		}
		if(!Tools.isBlank(channel)){
			msgContent.append("(渠道").append(time).append("日交易余额:)" + channel);
		}
		return msgContent.toString();
	}
	
	// 代理商信息  
	private String agentInfo(Date startdate,Date enddate) {
		StringBuffer sbf = new StringBuffer();
		try {
			List<LAgentConfig> agent = diaryInfoDao.findAllAgent();
			for (LAgentConfig agentCfg : agent) {
				BigDecimal money = diaryInfoDao.queryAgentBalanceDate(agentCfg.getAgentId(), startdate,enddate);
				log.info("---代理商余额:{}", money);
				if(null!=money){
					getDiayInfo(sbf, agentCfg.getAgentName(), String.valueOf(money));
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			log.info("代理商日销售额统计出错!!!!!!"+e.getMessage());
		}
		return sbf.toString();
	}

	private void getDiayInfo(StringBuffer sbf, String agentName,String money) {
		if(!Tools.isBlank(agentName)&&!Tools.isBlank(money)){
			sbf.append(agentName);
			sbf.append("余额 "+money+"元;");
		}
	}
	
	private String getChannelMoney(Date startTime,Date endTime){
		StringBuffer sb = new StringBuffer();
		HttpClient3 http = new HttpClient3();
		try {
			List<LChannelInfo> channel = diaryInfoDao.findAllChannel();
			if (!Tools.isBlank(channel)) {
				for (LChannelInfo channelarr : channel) {
					String channelname = channelarr.getChannelName();
					String d = 	new SimpleDateFormat("MM-dd HH:mm").format(new Date());
					String banlance="0";
					if(channelarr.getChannelCode().equals("100011")){
						try {
							if(null!=http.doPost(wuCaiUrl)){
								banlance = http.doPost(wuCaiUrl).replace("\"", "");
							}
							BigDecimal banlances=new BigDecimal(banlance);
							sb.append(channelname + ":" + banlances+ "元"+d).append(";");
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}else {
						banlance = diaryInfoDao.findAllChannelBanlance(channelarr.getChannelCode(),startTime,endTime)==null?"0":diaryInfoDao.findAllChannelBanlance(channelarr.getChannelCode(),startTime,endTime);
						BigDecimal banlances=new BigDecimal(banlance);
						BigDecimal value=new BigDecimal("100");
						sb.append(channelname + ":" + banlances.divide(value) + "元"+d).append(";");
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			log.info("渠道日销售额统计出错!!!!!!"+e.getMessage());
		}
		return sb.toString();
	}

	private Date getPreDate() {
		Date preDate = Tools.getPreDate(Tools.getCurrentDate());
		String preDateFmt = Tools.getFormatDateString(preDate, "yyyy-MM-dd 00:00:00");
		Date lastDate = Tools.parseShortDate(preDateFmt);
		return lastDate;
	}
	public static void main(String[] args) {
		Date preDate = Tools.getPreDate(Tools.getCurrentDate());
		String preDateFmt = Tools.getFormatDateString(preDate, "yyyy-MM-dd 00:00:00");
		Date lastDate = Tools.parseShortDate(preDateFmt);
		Date startdate = new ChannelBanlanceService().getPreDate();
		Date enddate = Tools.getCurrentShortFormatDate();
		System.out.println(lastDate);
		System.out.println(Tools.defaultFormat(startdate)+"==="+enddate);
	}
}
